package com.garlicts.framework.crawler.scheduler;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class SchedulerUtil {
	
	@Value("${zkAddress}")
	private String zkAddress;

	public JobDetail createJobDetail(String jobName, String jobContent, String executorName) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("executorBean", new ExecutorBean(executorName, zkAddress));
		
		return JobBuilder.newJob(RemoteQuartzJobBean.class)
				.usingJobData("executorName", executorName)
				.usingJobData("discoveryAddress", zkAddress)
				.usingJobData("jobContent", jobContent)
				.withIdentity(jobName).storeDurably().build();
		
	}
	
	public Trigger createJobTrigger(String jobName, String jobContent, String executorName, String cron) {
		
		CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
		return TriggerBuilder.newTrigger().forJob(createJobDetail(jobName, jobContent, executorName))
				.withIdentity("firstCrawlerTask")
				.withSchedule(cronScheduleBuilder)
				.build();
		
	}
	
}
